/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service.posServices;

import com.openbrew.openbrewrf.POSLineItems;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.AbstractFacade;
import tools.InventoryModifiers;
import tools.POSVolumeModifier;

/**
 *
 * @author dchapman
 */
@Stateless
@Path("com.openbrew.openbrewrf.poslineitems")
public class POSLineItemsFacadeREST extends AbstractFacade<POSLineItems> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public POSLineItemsFacadeREST() {
        super(POSLineItems.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public POSLineItems create(@WebParam(name = "poslineitems") POSLineItems entity) {
        entity = super.create(entity);
        //Remove Inventory from system.
        if(entity.getSupply()!=null){
            InventoryModifiers im = new InventoryModifiers();
            im.doSupplyRemovalWithoutAllocation(getEntityManager()
                    , entity.getSupply().getSupID(), (float)entity.getQty());
        }
        if(entity.getProduct()!=null){
            POSVolumeModifier vm = new POSVolumeModifier();
            vm.doVolumeRemoval(em, entity);
        }
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(POSLineItems entity) {
        //First check if we need to update supplies.
        final float oldQty = (float)super.find(entity.getId()).getQty();
        //Now make the changes.
        super.edit(entity);
        //Now check the new Qty.
        final float newQty = (float)super.find(entity.getId()).getQty();
        //check For Qty Changes
        if(oldQty != newQty){
            if(super.find(entity.getId()).getSupply()!=null){
                //update accordingly.
                InventoryModifiers im = new InventoryModifiers();
                im.doSupplyAdjustmentWithoutAllocation(getEntityManager()
                        , super.find(entity.getId()).getSupply().getSupID()
                        , oldQty, newQty);
            }
            if(super.find(entity.getId()).getProduct()!=null){
                //update accordingly.
                POSVolumeModifier vm = new POSVolumeModifier();
                vm.doVolumeChangeRemoval(em, entity, oldQty, newQty);
            }
        }
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        //Return item to inventory.
        if(super.find(id).getSupply()!=null){
            InventoryModifiers im = new InventoryModifiers();
            im.doSupplyReturnWithoutAllocation(getEntityManager()
                    , super.find(id).getSupply().getSupID()
                    , (float) super.find(id).getQty());
        }
        //Remove item from sale.
        super.remove(super.find(id));
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public POSLineItems find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<POSLineItems> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<POSLineItems> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}
